using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Decav.Gatsb.Application;
using Decav.Gatsb.Data;
using Decav.Gatsb.Web;

public partial class Friends : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!User.RequestingUserCanSeeProfile)
        {
            // If the user cannot see this profile, send him back to the person page
            // where he will just get the user tile.
            Response.Redirect(string.Format("~/Person.aspx?id={0}", User.Id));
        }

        if (!IsPostBack)
            SetupFriends();

        if (IsCurrentUsersFriends)
            ((IMasterPageTemplate)Master).SelectedPage = MainMenuNavigationButton.Friends;
        else
            ((IMasterPageTemplate)Master).SelectedPage = MainMenuNavigationButton.None;
    }

    private void SetupFriends()
    {
        if (IsCurrentUsersFriends)
        {
            mainHeader.InnerText = "Your Friends";
            showFriendWindowLink.Visible = true;
        }
        else
        {
            mainHeader.InnerHtml = string.Format("<a href=\"Person.aspx?id={0}\">{1}</a>'s Friends", User.Id, User.Name);
            showFriendWindowLink.Visible = false;
        }

        GatsbWebApplication.SetPageName(this, string.Format("{0}'s Friends", User.Name));

        Decav.Gatsb.User[] friends = GetFriends();

        if (friends.Length == 0)
        {
            friendList.Visible = false;
            noFriendsDiv.Visible = true;
            if (IsCurrentUsersFriends)
                noFriendsDiv.InnerText = "You have no friends. :-(";
            else
                noFriendsDiv.InnerText = string.Format("{0} has no friends. :-)", User.Name.FirstName);
        }
        else
        {
            friendList.Visible = true;
            noFriendsDiv.Visible = false;
            friendList.DataSource = friends;
            friendList.DataBind();
        }
    }

    private Decav.Gatsb.User[] GetFriends()
    {
        using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
            return adapt.GetUserFriends(UserId);
    }

    /// <summary>
    /// Gets the user to get friends for.
    /// </summary>
    public Decav.Gatsb.User User
    {
        get
        {
            if (_user == null)
            {
                using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
                    _user = adapt.GetUser(UserId);
            }

            return _user;
        }
    }

    private Decav.Gatsb.User _user;

    /// <summary>
    /// Gets the unique identifier of the user to get friends for.
    /// </summary>
    public Guid UserId
    {
        get
        {
            if (string.IsNullOrEmpty(Request.QueryString["id"]))
                return GatsbApplication.Current.CurrentUser.Id;

            return new Guid(Request.QueryString["id"]);
        }
    }

    /// <summary>
    /// Gets if we're looking at the current users friends.
    /// </summary>
    public bool IsCurrentUsersFriends
    {
        get
        {
            return (string.IsNullOrEmpty(Request.QueryString["id"]));
        }
    }

    protected void AddFriendButton_Click(object sender, EventArgs e)
    {
        if (addFriendTextBox.Text.Trim() == string.Empty)
            return;

        Response.Redirect(GatsbWebApplication.Current.CreateSearchUrl(addFriendTextBox.Text, true, false), true);
    }
}
